package com.mystyle.le.weekproject.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * 流式布局
 * Created by le on 2016/10/12.
 */
public class FlowLayoutView extends ViewGroup{

    public FlowLayoutView(Context context) {
        super(context);
    }

    public FlowLayoutView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }



    //获取ViewGroup中所有子元素的属性
    //否则只获取ViewGroup 的属性
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(),attrs);
    }

    /*
    * 测量控件本身的大小   宽和高   根据子内容获取
    * */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //获取父布局的模式和尺寸
        int sizeWith = MeasureSpec.getSize(widthMeasureSpec);
        int modeWith = MeasureSpec.getMode(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
        //记录Wrap_content的宽高
        int width = 0;
        int height = 0;

        //每一行的宽和高
        int lineWidth = 0;
        int lineHeight =0;
        //获取子元素的数量
        int childCount = getChildCount();
        //进行遍历子元素
        for (int i =0;i<childCount;i++){
            //获取每一个子元素
            View childAt = getChildAt(i);
            //测量每一个子元素
            measureChild(childAt,widthMeasureSpec,heightMeasureSpec);
            //获取每个子元素的属性
            MarginLayoutParams marginLayoutParams = (MarginLayoutParams) childAt.getLayoutParams();
            //获取当前子元素的宽和高
            int childWidth = childAt.getWidth()+marginLayoutParams.leftMargin+marginLayoutParams.rightMargin;
            int childHeight = childAt.getMeasuredHeight()+marginLayoutParams.bottomMargin+marginLayoutParams.topMargin;
            //如果每一行的宽度+子元素的宽>总宽-左边宽度-右边宽度  换行
            if (lineWidth+childWidth>getWidth()-getPaddingLeft()-getPaddingRight()){
                width = Math.max(lineWidth,childWidth);
                lineWidth = childWidth;//重新开一行  当前行的宽度
                //叠加之前的高度
                height+=lineHeight;
                lineHeight = childHeight;

            }else {//不换行
                //记录当前行的宽
                lineWidth+=childWidth;
                lineHeight = Math.max(lineHeight,childHeight);
            }
            if (i==childCount-1){//判断是否是最后一个元素
                width = Math.max(width,lineWidth);
                height+=lineHeight;
            }
            //设置ViewGrop最终宽高
            setMeasuredDimension(
                    modeWith==MeasureSpec.EXACTLY?sizeWith:width+getPaddingRight()+getPaddingLeft(),
                    modeHeight==MeasureSpec.EXACTLY?sizeHeight:height+getPaddingBottom()+getPaddingTop()
            );

        }

    }
    //记录所有子元素   按行存取
    List<List<View>> mAllViews = new ArrayList<>();
    //记录每一行中最大的高度
    private List<Integer> mLineHeight = new ArrayList<>();

    //摆放子View
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        //为了多次执行时数据正确
        mAllViews.clear();
        mLineHeight.clear();
        //获取父控件的宽度
        int width = getWidth();
        //记录每一行中的宽和高
        int lineWidth = 0;
        int lineHeight = 0;

        //记录一行中的view
        List<View> lineView = new ArrayList<>();
        //获取子元素的个数
        int cCount = getChildCount();
        //遍历所有子元素
        for (int i =0;i<cCount;i++){
            View childAt = getChildAt(i);
            //获取每个子元素的属性
            MarginLayoutParams marginLayoutParams = (MarginLayoutParams) childAt.getLayoutParams();
            //获取当前子元素的宽和高
            int childWidth = childAt.getMeasuredWidth()+marginLayoutParams.leftMargin+marginLayoutParams.rightMargin;
            int childHeight = childAt.getMeasuredHeight()+marginLayoutParams.bottomMargin+marginLayoutParams.topMargin;

            //如果每一行的宽度+子元素的宽>总宽-左边宽度-右边宽度  换行
            if (lineWidth+childWidth>width-getPaddingLeft()-getPaddingRight()){
                mLineHeight.add(lineHeight);
                mAllViews.add(lineView);

                //让数据重置
                lineWidth = 0;
                lineView = new ArrayList<>();
            }else {//不换行
                //记录当前行的宽
                lineWidth+=childWidth;
                lineHeight = Math.max(lineHeight,childHeight);
                lineView.add(childAt);
            }
        }
        //处理元素   数据以每行的形式处理
        mLineHeight.add(lineHeight);
        mAllViews.add(lineView);
        //获取每个View的padding
        int left = getPaddingLeft();
        int top = getPaddingTop();
        //获取有多少行
        int lineNum = mAllViews.size();

        //展示每一行
        for (int i =0;i<lineNum;i++){
            lineHeight = mLineHeight.get(i);
            lineView   = mAllViews.get(i);
            //遍历每一行
            // 取出 每一行的 所有的View
            lineView = mAllViews.get(i);
            lineHeight = mLineHeight.get(i);
            // 循环遍历  每一行的View
            for(int j=0;j<lineView.size();j++){
                View child = lineView.get(j);
                MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

                int lc = left+lp.leftMargin;
                int tc = top+lp.topMargin;
                int rc = lc+child.getMeasuredWidth();
                int bc = tc+child.getMeasuredHeight();
                //摆放 子View的位置
                child.layout(lc, tc, rc, bc);
                //叠加   前一个View 的 实际宽度
                left += child.getMeasuredWidth()+lp.leftMargin+lp.rightMargin;
            }
            //换行了 重置
            left = getPaddingLeft();
            top+=lineHeight;
        }

    }
}


